/**
 * ------------------------------------------------------------------------------------------
 * Copyright (c): Company, 2012 This software is the
 * proprietary information of FHWS, Würzburg All Right Reserved.
 * ------------------------------------------------------------------------------------------
 *
 *
 * @project: Vorlesungplan
 * @copyright FHWS 2012
 * @link       http://mussnocheingeragenwerden.de
 * @package    tt.Servlet
 *
 * SVN revision information:
 *
 * @version $Rev$:
 * @author $Author$:
 * @date $Date$:
 */

package tt.Servlet;

import com.itextpdf.text.*;
import com.itextpdf.text.pdf.PdfWriter;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import tt.Exception.EntityServiceException;
import tt.VorlesungenService.EntityService;
import tt.VorlesungenService.PDF;
import tt.model.Courseschedule;
import tt.model.Lecturedate;

@WebServlet(name = "PDFServlet", urlPatterns =
{
   "/PDFServlet"
})
public class PDFServlet extends HttpServlet
{

   @EJB
   private EntityService service;

   @Override
   protected void doGet(HttpServletRequest request, HttpServletResponse response)
           throws ServletException, IOException
   {

      Font fontHead = new Font(Font.FontFamily.HELVETICA, 20, Font.BOLD);
      Font fontNormal = new Font(Font.FontFamily.HELVETICA, 10, Font.NORMAL);

      try
      {

         String id = request.getParameter("schedule");
         Courseschedule mySchedule = null;
         try
         {
            mySchedule = service.getCourseScheduleByIDCourseSchedule(id);
            Collection<Lecturedate> myLectureDates = mySchedule.getLecturedateCollection();
         } catch (EntityServiceException ex)
         {
            Logger.getLogger(PDFServlet.class.getName()).log(Level.SEVERE, null, ex);
         }

         if (mySchedule != null)
         {

            Lecturedate temp = null;


            // step 1 create a new Page DIN A4
            Document document = new Document(PageSize.A4.rotate());

            // step 2 create a new PDFWriter to manipulate the dokument
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            PdfWriter.getInstance(document, baos);

            // step 3 open the dokument
            document.open();

            // step 4 add content
            String baseURL = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath();

            //Überschrift
            Paragraph heading = new Paragraph("Vorlesungsplan ", fontHead);
            heading.setAlignment(1);
            document.add(heading);

            Paragraph ID = new Paragraph("ID des Vorlesungsplanes: " + mySchedule.getIdcourseschedule(), fontNormal);
            ID.setAlignment(1);
            document.add(ID);


            document.add(Chunk.NEWLINE);
            //Logo


            Image logo = Image.getInstance(new URL(baseURL + "/resources/images/39653_fh-logo-klein.jpg"));
            logo.scalePercent(60);
            logo.setAbsolutePosition(10, 510);
            document.add(logo);



            PDF PDFLectureTable = new PDF(mySchedule);
            document.add(PDFLectureTable.createTable());


            //Kommentare nach der Table
            Paragraph scheduleid = new Paragraph(baseURL, fontNormal);
            scheduleid.setAlignment(2);
            document.add(scheduleid);

            Date heute = new Date();
            SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy HH:mm");
            Paragraph gewaehr = new Paragraph("Angaben ohne Gewähr. Stand: " + sdf.format(heute) + " Uhr", fontNormal);
            gewaehr.setAlignment(2);
            document.add(gewaehr);
            Paragraph verbindliche = new Paragraph("Eine verbindliche Version finden sie im Aushang der Fakultät!", fontNormal);
            verbindliche.setAlignment(2);
            document.add(verbindliche);




            // step 5 close dokument
            document.close();

            // setting some response headers
            response.setHeader("Expires", "0");
            response.setHeader("Cache-Control",
                               "must-revalidate, post-check=0, pre-check=0");
            response.setHeader("Pragma", "public");
            // setting the content type
            response.setContentType("application/pdf");
            // the contentlength
            response.setContentLength(baos.size());
            // write ByteArrayOutputStream to the ServletOutputStream
            OutputStream os = response.getOutputStream();
            baos.writeTo(os);
            os.flush();
            os.close();
         } else
         {
            //Fehler - konnte kein mySchedule Objekt erstellen log????
            Logger.getLogger(PDFServlet.class.getName()).log(Level.WARNING, "PDF: Konnte kein mySchedule objekt erstellen!");
         }//end if
      } catch (Exception e)
      {
      }
   }
}
